package com.ruoyi.system.service.impl;

import com.ruoyi.common.config.oss.OssContext;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.oss.OssUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.service.AdminUploadService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;


@Service
public class AdminUploadServiceImpl implements AdminUploadService {


    @Override
    public Map<String, String> uploadFile(MultipartFile file) {
        Map<String ,String> map=new HashMap<>();
        // 上传文件路径
        LocalDateTime now = LocalDateTime.now();
        //重组文件名生成唯一名
        String filename = file.getOriginalFilename();
        //文件类型
        String substring = filename.substring(filename.lastIndexOf("."));

        //文件名
        StringBuffer fileNameResult =new StringBuffer("");
        fileNameResult.append(LocalDateTime.now().toInstant(ZoneOffset.ofHours(+8)).toEpochMilli());
        fileNameResult.append("_");
        fileNameResult.append(UUID.randomUUID());
        fileNameResult.append(substring);
        String url=now.getYear()+"/"+now.getMonth()+"/"+now.getDayOfMonth()+"/"+fileNameResult.toString();
      try {
            OssUtil.OssUploadFileStream(url,file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("文件上传异常");
        }
        map.put("url", OssContext.getFilePath()+url);
        map.put("fileName",fileNameResult.toString());

        return map;
    }
}
